Skip to content

fix: resolve cyclic structure serialization crash in token counter - #839

Closed
OmarAlaaeldein wants to merge 8736 commits into
CodebuffAI:mainfrom
OmarAlaaeldein:fix/zod-cyclic-structure-crash
Closed

fix: resolve cyclic structure serialization crash in token counter#839
OmarAlaaeldein wants to merge 8736 commits into
CodebuffAI:mainfrom
OmarAlaaeldein:fix/zod-cyclic-structure-crash

Conversation

@OmarAlaaeldein

Copy link
Copy Markdown

This PR fixes a TypeError crash: JSON.stringify cannot serialize cyclic structures when the token count API fails and falls back to local estimation.

  • Implement safeJsonStringify in token-counter.ts to handle circular references.
  • Sanitize the inputSchema inside toolDefinitions at creation time using JSON.parse(safeJsonStringify(schema)) so it is a plain, serializable object.
  • This also prevents circular structure errors when writing the chat run state to disk (run-state.json).

Source: CodebuffAI/freebuff-private@68270a672f2c08002891aeeddb5e0f057d8f9bdb
Source: CodebuffAI/freebuff-private@c470cc009bb9afe9f383dea6cec2de397f5c8c5f
Source: CodebuffAI/freebuff-private@f80116428d5a19f8b3d627264e27e7645c8ba7b4
Source: CodebuffAI/freebuff-private@18572a0e9a8fa966695de53e9f7444e8bf03e020
Source: CodebuffAI/freebuff-private@f618ec1759c19396db8c349b5ca238c453cb55ad
Source: CodebuffAI/freebuff-private@73bb0d55b8a26ef2cab44d3dd46fca2c486558e8
Source: CodebuffAI/freebuff-private@9a11364ff22a275f162397c045654a78804fb5ec
Source: CodebuffAI/freebuff-private@24ceceb5e64cfd0a9389085b11e233c8002073d8
Source: CodebuffAI/freebuff-private@9862bf289b33d01ffbd3734757dbaa3d2c6d8934
Source: CodebuffAI/freebuff-private@9862bf289b33d01ffbd3734757dbaa3d2c6d8934
Source: CodebuffAI/freebuff-private@e79560f4330e03548c8bbe5fc53ccccea467eecd
Source: CodebuffAI/freebuff-private@2be3a466f022dcdd6c1c64279f857e3bbc05b633
Source: CodebuffAI/freebuff-private@50e3ec204cdd9bbe469161113d5dbd01c89c643f
Source: CodebuffAI/freebuff-private@dc12c70c05c37325ce08766634d213c596a4c1fe
Source: CodebuffAI/freebuff-private@ee44ade2b2432cdb77d3db64a0294176fbb291b1
Source: CodebuffAI/freebuff-private@c08ecb40ccf84131f4c3217aacc4f13f97e53c1a
Source: CodebuffAI/freebuff-private@3d0061af3e237046ad2f488110c02cc3187a5ec5
Source: CodebuffAI/freebuff-private@35ca20373a226fdf6e496388f4d4c97fb540822e
Source: CodebuffAI/freebuff-private@bf262b32c55a625597e996586fc29592a33cc2b2
Source: CodebuffAI/freebuff-private@1c17c26ca0346855819b93572f9acde54315a7d5
Source: CodebuffAI/freebuff-private@2e8e98b80328ded3841e7a29dd775babf0601731
Source: CodebuffAI/freebuff-private@bf044368bd1473abc9b58a57876302d97d5c0049
Source: CodebuffAI/freebuff-private@060e1f152335e26c2c03a551ff87175aaa141ccb
Source: CodebuffAI/freebuff-private@c4214d3a43c83a9ce0b75effd8e983a8903f1841
Source: CodebuffAI/freebuff-private@c4fa5e278eb9e01edbd2fe118f946a503d9681f7
Source: CodebuffAI/freebuff-private@cbd9b7f4060e41040434cb0909f47608735006e3
Source: CodebuffAI/freebuff-private@b7679a14a3607bb04cd463e104236032bce1d433
Source: CodebuffAI/freebuff-private@f5bb453fcbc9befc8418a0a2a910d18b9f862902
Source: CodebuffAI/freebuff-private@e7c025c3b02dc7bc4979d0edebab4ce55a3635ef
Source: CodebuffAI/freebuff-private@4e5cb19bc3c495dac3347852a9aa58028bc72343
Source: CodebuffAI/freebuff-private@01dd70449d6274cb94abacb49324d6784a05c7a5
Source: CodebuffAI/freebuff-private@48a20110f3bd5f0fc1aa4fd4714414934ccb62fc
Source: CodebuffAI/freebuff-private@85b68a24a30474b3f39ce0b77748c8e4bdbb072b
Source: CodebuffAI/freebuff-private@95a3ae1fed2426a0f5a47ef71e51bcbc36060cc2
Source: CodebuffAI/freebuff-private@13a3c25821fe9d55a392b152074cc3408a2385f8
Source: CodebuffAI/freebuff-private@e0c097a37c126690463e217086e6392993ecc160
Source: CodebuffAI/freebuff-private@cbb8d684378966a2eaeee59f4ef25e4161194137
Source: CodebuffAI/freebuff-private@ff4e7a3083ccb9ef1bd307ab80bff8e4b7c16b59
Source: CodebuffAI/freebuff-private@a881c143392953c7d855d73373f45a13a5ea9f72
Source: CodebuffAI/freebuff-private@9928303688d84e0d2db9b7947e3e31666e5a5524
Source: CodebuffAI/freebuff-private@08be8980d30621c62a5dbb2c9efabac0ae6abbeb
Source: CodebuffAI/freebuff-private@72547e1e8db6152b7ad23d624fee83502102d4a8
Source: CodebuffAI/freebuff-private@503a8ac46812fe55ed0047aba39435805b8b6e28
Source: CodebuffAI/freebuff-private@9eb310e037653ac898900144b247cad43baff187
Source: CodebuffAI/freebuff-private@86f3bd2ed9caa2b590895b1077d86f14a44dee4c
Source: CodebuffAI/freebuff-private@a31d9a467ba9fa6f81034a729136b63dc9de2ca5
Source: CodebuffAI/freebuff-private@361090e80e487ecae0fdfd49ef339ac4e01c6e7d
Source: CodebuffAI/freebuff-private@c920b3716c6f0b837008e5312796065277112ed9
Source: CodebuffAI/freebuff-private@c0ffc5fe85e218f98057d2fc3651c3217b4f64f3
Source: CodebuffAI/freebuff-private@15143092a3607cf34e034869296afdfbbcbc0e6e
Source: CodebuffAI/freebuff-private@ff7ce305b29077c8b0707290e05c7e8a01a86882
@codebuff-team codebuff-team added the stale No activity after a maintainer request; queued for closing label Aug 23, 2026
@codebuff-team

Copy link
Copy Markdown
Contributor

Marking this stale - there has been no activity here for 50 days. It will close in 7 days unless someone comments.

This is backlog upkeep, not a verdict on the pull request. A single comment keeps it open, and anything closed this way can be reopened.

When the token count API call fails or is not available, the CLI fallback logic
serializes  using . However,
contains raw Zod schema objects (), which have circular references,
causing a  TypeError.

Changes:
- Added a  utility to strip circular references from objects.
- Sanitize the  inside  at creation time using
   so it is a plain, serializable object.
  This also prevents similar serialization failures when saving run state to disk.
@OmarAlaaeldein
OmarAlaaeldein force-pushed the fix/zod-cyclic-structure-crash branch from 85753fa to 7adbf86 Compare August 23, 2026 20:09
@OmarAlaaeldein

Copy link
Copy Markdown
Author

Rebased onto current main and tightened the serialization fix. countTokensJson keeps its existing JSON-string serialization behavior; safeJsonStringify handles circular values, drops function properties using JSON semantics, and is used when persisting tool schemas. Added regression coverage for circular references, function properties, and string serialization. Verified with: bun test packages/agent-runtime/src/util/tests/token-counter.test.ts (9 passing).

@codebuff-team codebuff-team removed the stale No activity after a maintainer request; queued for closing label Aug 31, 2026
@victorxheng

Copy link
Copy Markdown

Apologies — this PR was auto-closed by GitHub when we force-pushed a history rewrite of this repository (repository maintenance; every commit SHA changed). That was not a judgment on this PR, and GitHub does not allow us to reopen it because the commits it was based on no longer exist in the new history.

If you'd like to continue with this change: rebase your branch onto the new main (or recreate it from a fresh clone) and open a new PR — feel free to link back to this one for context, and we'll pick up the review there.

Sorry for the churn, and thanks for contributing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants